package com.vil.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vil.common.ServerResponse;
import com.vil.entity.Admin;
import com.vil.mapper.AdminMapper;
import com.vil.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper mapper;
    @Override
    public ServerResponse insertSelective(Admin record) {
        record.setJoin_time(new Date());
        int i = mapper.insertSelective(record);
        if (i>0){
            return ServerResponse.buildSuccessMsg("新增成功");
        }
        return ServerResponse.buildErrorMsg("新增失败");
    }

    @Override
    public ServerResponse deleteByPrimaryKey(Long id) {
        int i = mapper.deleteByPrimaryKey(id);
        if (i>0){
            return ServerResponse.buildSuccessMsg("删除成功");
        }
        return ServerResponse.buildErrorMsg("删除失败");
    }

    @Override
    public ServerResponse updateByPrimaryKeySelective(Admin record) {
        int i = mapper.updateByPrimaryKeySelective(record);
        if (i>0){
            return ServerResponse.buildSuccessMsg("修改成功");
        }
        return ServerResponse.buildErrorMsg("修改失败");
    }

    @Override
    public ServerResponse getListByManyConditions(Integer pageIndex, Integer pageSize, Admin admin) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Admin> list = mapper.getListByManyConditions(admin);
        PageInfo pageInfo = new PageInfo(list);
        return ServerResponse.buildSuccessData(pageInfo);
    }

    @Override
    public ServerResponse loginCheck(String login_code, String password) {
        Admin admin = mapper.loginCheck(login_code, password);

        System.out.println(admin.toString());
        if (admin==null){
            return ServerResponse.buildErrorMsg("该管理员不存在");
        }
        return ServerResponse.buildSuccessData(admin);
    }

    @Override
    public ServerResponse getListByManyCondition(Integer pageIndex, Integer pageSize, String login_code) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Admin> list = mapper.getListByManyCondition(login_code);
        PageInfo adminPageInfo = new PageInfo(list);

        return ServerResponse.buildSuccessData(adminPageInfo);
    }

    @Override
    public ServerResponse getById(Long id) {
        Admin admin = mapper.selectByPrimaryKey(id);
        if (admin==null){
            return ServerResponse.buildErrorMsg("不存在该管理员信息");
        }
        return ServerResponse.buildSuccessData(admin);
    }

    @Override
    public ServerResponse deleteMany(Long[] ids) {
        int i = mapper.deleteMany(ids);
        if (i>0){
            return ServerResponse.buildSuccessMsg("批量删除成功");
        }
        return ServerResponse.buildErrorMsg("批量删除失败");
    }

    @Override
    public ServerResponse isExist(String login_code) {
        int exist = mapper.isExist(login_code);
        if (exist>0){
           return ServerResponse.buildErrorMsg("该登录名已存在，请重新输入");
        }
        return ServerResponse.buildSuccessMsg("该用户名可以使用");
    }
}
